package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import entity.User;
import net.sf.json.JSONArray;

public class TCPServer {
	public static void main(String[] args) throws Exception{
		List<User> loginUserList=new ArrayList<User>();
		for (int i = 0; i < 5; i++) {
			User loginUser=new User();
			loginUser.setUsername("wangben"+i);
			loginUser.setPassword("123");
			loginUserList.add(loginUser);
		}
		
		ServerSocket server=new ServerSocket(7001);
//		Map<String,User> map=new HashMap<String,User>();
		List<Map<String,User>> list=new ArrayList();
		while(true){
			
			Socket socket=server.accept();
			OutputStream os=socket.getOutputStream();
			BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String encoding=reader.readLine();
			String username=reader.readLine();
			String password=reader.readLine();
			
			if(islogin(username,password,loginUserList)){
				
				os.write(("loginsuccess"+"\n").getBytes());
				new Thread(new Client(socket,list,os,encoding,username,reader)).start();
			}else
			{
				os.write(("loginfail"+"\n").getBytes());
				//socket.close();
			}
		}
	}
	
	public static boolean islogin(String name,String pswd,List<User> loginUserList){
		boolean islogin=false;
		for (int i = 0; i < loginUserList.size(); i++) {
			User loginUser=loginUserList.get(i);
			if((loginUser.getUsername().equals(name))&&(loginUser.getPassword().equals(pswd))){
				islogin=true;
			}
		}
		return islogin;
	}
}

class Client implements Runnable{
	private Socket socket;
	private List list;
	private OutputStream os;
	private String encoding;
	private String username;
	private BufferedReader reader;
	public Client(Socket socket,List list,OutputStream os,String encoding,String username,BufferedReader reader) {
		super();
		this.socket = socket;
		this.list=list;
		this.os=os;
		this.encoding=encoding;
		this.username=username;
		this.reader=reader;
	}

	@Override
	public void run() {
		try {
//			OutputStream os=socket.getOutputStream();
//			BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//			String encoding=reader.readLine();
//			String username=reader.readLine();
			Map<String,User> map=new HashMap<String,User>();
			User user=new User();
			user.setEncoding(encoding);
			user.setSocket(socket);
			user.setUsername(username);
			map.put(username, user);
			list.add(map);
			JSONArray jsonarr=JSONArray.fromObject(list);
			os.write((jsonarr.toString()+"\n").getBytes());
			
			if(list!=null){
				for (int i = 0; i < list.size(); i++) {
					Map<String,User> map2=(Map<String, User>) list.get(i);
					Set<String> set = map2.keySet();
					String username2=null;
					for (String string : set) {
						username2=string;
						
					}
					
				  User user2=map2.get(username2);
				  Socket socket2=user2.getSocket();
				  String encoding2=user2.getEncoding();
				  OutputStream os2=socket2.getOutputStream();
				  os2.write("haoyoulibiao\n".getBytes(encoding2));
				  JSONArray jsonarr2=JSONArray.fromObject(list);
				  os2.write((jsonarr2.toString()+"\n").getBytes());
//				  System.out.println("-----");
				}
				}
			
			while(true){
				String ml=reader.readLine();
//				System.out.println(ml);
				if("qf".equals(ml)){
					String msg=reader.readLine();
					System.out.println("server"+msg);
					if(list!=null){
					for (int i = 0; i < list.size(); i++) {
						Map<String,User> map2=(Map<String, User>) list.get(i);
						Set<String> set = map2.keySet();
						String username2=null;
						for (String string : set) {
							username2=string;
							
						}
						System.out.println(username);
					  User user2=map2.get(username2);
					  Socket socket2=user2.getSocket();
					  String encoding2=user2.getEncoding();
					  OutputStream os2=socket2.getOutputStream();
					  os2.write("qfandzf\n".getBytes(encoding2));
					  os2.write((msg+"\n").getBytes(encoding2));
					}
					}
				}if("dakaichuangkou".equals(ml)){
					String hisusername=reader.readLine();
					System.out.println("鎵撳紑绐楀彛锛�"+hisusername);
					String myusername=reader.readLine();
					for (int i = 0; i < list.size(); i++) {
						Map<String, User> map2 = (Map<String, User>) list.get(i);
						Set<String> set = map2.keySet();
						String username = null;
						for (String str : set) {
							username = str;
						}
						if (username.equals(hisusername)) {
							User user2 = map2.get(hisusername);
							Socket socket2=user2.getSocket();
							String encoding2=user2.getEncoding();
							OutputStream os2=socket2.getOutputStream();
							os2.write("dakaidl\n".getBytes(encoding2));
							os2.write((myusername+"\n").getBytes(encoding2));
						}
					}
					 
				}if("df".equals(ml)){
					String hisusername=reader.readLine();
					String myusername=reader.readLine();
					String msg=reader.readLine();
					for (int i = 0; i < list.size(); i++) {
						Map<String, User> map2 = (Map<String, User>) list.get(i);
						Set<String> set = map2.keySet();
						String username = null;
						for (String str : set) {
							username = str;
						}
						if (username.equals(hisusername)) {
							User user3 = map2.get(username);
							Socket socket3=user3.getSocket();
							String encoding3=user3.getEncoding();
							OutputStream os3=socket3.getOutputStream();
							os3.write("danliao\n".getBytes(encoding3));
							os3.write((myusername+"\n").getBytes(encoding3));
							os3.write((msg+"\n").getBytes(encoding3));
						}
					}
				}if("df2".equals(ml)){
					String hisusername=reader.readLine();
					String myusername=reader.readLine();
					String msg=reader.readLine();
					for (int i = 0; i < list.size(); i++) {
						Map<String, User> map2 = (Map<String, User>) list.get(i);
						Set<String> set = map2.keySet();
						String username = null;
						for (String str : set) {
							username = str;
						}
						if (username.equals(hisusername)) {
							User user3 = map2.get(username);
							Socket socket3=user3.getSocket();
							String encoding3=user3.getEncoding();
							OutputStream os3=socket3.getOutputStream();
							os3.write("exdanliao\n".getBytes(encoding3));
							os3.write((myusername+"\n").getBytes(encoding3));
							os3.write((msg+"\n").getBytes(encoding3));
						}
					}
				}if("zd".equals(ml)){
					String hisusername=reader.readLine();
					String myusername=reader.readLine();
					System.out.println(hisusername+"  鎶栧姩娴嬭瘯  "+myusername);
					for (int i = 0; i < list.size(); i++) {
						Map<String, User> map2 = (Map<String, User>) list.get(i);
						Set<String> set = map2.keySet();
						String username = null;
						for (String str : set) {
							username = str;
						}
						if (username.equals(hisusername)) {
							User user3 = map2.get(username);
							Socket socket3=user3.getSocket();
							String encoding3=user3.getEncoding();
							OutputStream os3=socket3.getOutputStream();
							os3.write("dfzd\n".getBytes(encoding3));
							os3.write((myusername+"\n").getBytes(encoding3));
						}
					}
				}if("exzd".equals(ml)){
					String hisusername=reader.readLine();
					String myusername=reader.readLine();
					for (int i = 0; i < list.size(); i++) {
						Map<String, User> map2 = (Map<String, User>) list.get(i);
						Set<String> set = map2.keySet();
						String username = null;
						for (String str : set) {
							username = str;
						}
						if (username.equals(hisusername)) {
							User user3 = map2.get(username);
							Socket socket3=user3.getSocket();
							String encoding3=user3.getEncoding();
							OutputStream os3=socket3.getOutputStream();
							os3.write("exdfzd\n".getBytes(encoding3));
							os3.write((myusername+"\n").getBytes(encoding3));
						}
					}
				}
				
				
			}

//			os.close();
//			reader.close();
//			socket.close();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}